﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using Entities;
using DevExpress.XtraReports.UI;


namespace QLTTTH
{
    public partial class frmDangKi : DevExpress.XtraEditors.XtraForm
    {
        public frmDangKi()
        {
            InitializeComponent();
        }
        public void BatNutLuu()
        {
            btnLuu.Enabled = true;
        }
        public DangKy GetDangKi()
        {
            DangKy dk = new DangKy();
            if (!txtMaDK.Text.ToString().Equals(""))
            {
                dk.MaDangKy = int.Parse(txtMaDK.Text.ToString());
            }
            dk.MaHV = int.Parse(loupHocVien.EditValue.ToString());
            dk.NgayDangKy = DateTime.Parse(dtpNgayDK.Text.ToString());
            dk.MaLop = int.Parse(loupLopHoc.EditValue.ToString());
            dk.MaKH = int.Parse(loupKhachHang.EditValue.ToString());
            dk.LoaiDangKy = txtLoaiDK.Text;
            return dk;
        }
        public delegate void SendData(DangKy dk);

        public SendData sendData;

        private void btnLuu_Click(object sender, EventArgs e)
        {
              if (sendData != null)
              {
                 
                  sendData(GetDangKi());
                  DialogResult = DialogResult.OK;
                  this.Close();
               
              }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            ResetControl();
            this.Close();
        }
        private void ResetControl()
        {
            this.loupHocVien.ResetText();
            this.dtpNgayDK.ResetText();
            this.loupLopHoc.ResetText();
            this.loupKhachHang.ResetText();
            this.txtLoaiDK.ResetText();
        }

        private void frmDangKi_Load(object sender, EventArgs e)
        {
            //var dsHV = HocVienManager.LayDanhSachHocVien();
            //if (dsHV != null)
            //{
            //    loupHocVien.Properties.DataSource = dsHV;
            //    loupHocVien.Properties.ValueMember = "MaHV";
            //    loupHocVien.Properties.DisplayMember = "HoTen";
            //}
            LoadDsDangKy();
            InitControl();
     
        }

        private void InitControl()
        {
            var dsHV = HocVienManager.LayDanhSachHocVien();
            if (dsHV.Success)
            {
                loupHocVien.Properties.DataSource = dsHV.Result;
                loupHocVien.Properties.ValueMember = "MaHV";
                loupHocVien.Properties.DisplayMember = "HoTen";
            }
            var dsLH = LopHocManager.LayDanhSachLopHoc();
            if (dsLH.Success)
            {
                loupLopHoc.Properties.DataSource = dsLH.Result;
                loupLopHoc.Properties.ValueMember = "MaLop";
                loupLopHoc.Properties.DisplayMember = "TenLop";
            }
            var dsKH = KhoaHocManager.LayDanhSachKhoaHoc();
            if (dsKH.Success)
            {
                loupKhachHang.Properties.DataSource = dsKH.Result;
                loupKhachHang.Properties.ValueMember = "MaKH";
                loupKhachHang.Properties.DisplayMember = "TenKH";
            }
        }

        private void LoadDsDangKy()
        {
            var dsDK = DangKiManager.LayDanhSachDangKy();
            if (dsDK.Success)
            {
                LoadDsDangKy(dsDK);
            }
        }

        private void LoadDsDangKy(ActionResultAsList<DangKy> dsDK)
        {
            //grvDangKy.DataSource = dsDK.Result;
            var aResult = DangKiManager.LayDanhSachDangKy();
            if (aResult.Success)
                gridDanhSach.DataSource = aResult.Result;
        }
       
       
    }
}